<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/8
 * Time: 14:07
 */

namespace Home\Controller;

use JPush\Client;
use JPush\Exceptions\APIConnectionException;
use JPush\Exceptions\APIRequestException;
use think\Controller;

class RechargeController extends Controller
{

    //初始化方法
    protected function _initialize()
    {
        //检测用户是否登陆 并定义全局变量
        if(isset($_SESSION['msg']['id'])){
            define('UID', $_SESSION['msg']['id']);
        } else {
            $this->redirect('/index.php/Home/Login/login');
        }
    }


    public function index()
    {
        if (IS_AJAX) {
            $tel = $_POST['tel'];//客户手机号
            $phone = $_POST['phone'];//推荐人手机号
            $money = $_POST['money']; // 充值金额
            $recharge_no= $_POST['recharge_no']; // 充值会员编号
            $job_no= $_POST['job_no']; // 推荐人工号
        }

        //获取各等级奖金数目
        $params = $this->get_pratio();
        //var_dump($params);die;
        //判断推荐充值用户是否存在,并且判断推荐用户上面有几级用户
        $userinfo = check_user_exist($phone);
        if($userinfo['id']>0){
            /*层级*/
            //先给自己发
            //id   money  level phone
            $userself = send_charge($userinfo['id'],$params['rate_0'],0,$tel,$money,$phone,$recharge_no,$job_no);//给推荐人发放奖金
            if($userself){
                if($userinfo['agent_id']>0){
                    //奖金发放
                    $agent = send_charge($userinfo['agent_id'],$params['rate_1'],1,$tel,$money,$phone,$recharge_no,$job_no);//给推荐人上级发放奖金
                    if($agent){
                        if($userinfo['upper_agent_id']>0){
                            $grandpa = send_charge($userinfo['upper_agent_id'],$params['rate_2'],2,$tel,$money,$phone,$recharge_no,$job_no);//给推荐人上上级发放奖金
                            if($grandpa){
                                if($userinfo['upper_upper_agent_id']>0){
                                    $greatpa = send_charge($userinfo['upper_upper_agent_id'],$params['rate_3'],3,$tel,$money,$phone,$recharge_no,$job_no);//给推荐人上上上级发放奖金
                                    if($greatpa){
                                        $this->send_push($userinfo['id'],$userinfo['tel'],$params['rate_0']);
                                        $this->send_push($userinfo['agent_id'],$userinfo['agent_tel'],$params['rate_1']);
                                        $this->send_push($userinfo['upper_agent_id'],$userinfo['upper_agent_tel'],$params['rate_2']);
                                        $this->send_push($userinfo['upper_upper_agent_id'],$userinfo['upper_upper_agent_tel'],$params['rate_3']);
                                        $ary = array('status'=>2,'str'=>'充值成功！');
                                        $this->ajaxReturn($ary);
                                    }
                                }else{
                                    //echo $params['rate_0']."---".$params['rate_1']."---".$params['rate_2'];die;
                                    $this->send_push($userinfo['id'],$userinfo['tel'],$params['rate_0']);
                                    $this->send_push($userinfo['agent_id'],$userinfo['agent_tel'],$params['rate_1']);
                                    $this->send_push($userinfo['upper_agent_id'],$userinfo['upper_agent_tel'],$params['rate_2']);
                                    $ary = array('status'=>2,'str'=>'充值成功！');
                                    $this->ajaxReturn($ary);
                                }
                            }
                        }else{
                            $this->send_push($userinfo['id'],$userinfo['tel'],$params['rate_0']);
                            $this->send_push($userinfo['agent_id'],$userinfo['agent_tel'],$params['rate_1']);
                            $ary = array('status'=>2,'str'=>'充值成功！');
                            $this->ajaxReturn($ary);
                        }
                    }
                }else{
                    $this->send_push($userinfo['id'],$userinfo['tel'],$params['rate_0']);
                    $ary = array('status'=>2,'str'=>'充值成功！');
                    $this->ajaxReturn($ary);
                }
            }

        }else{
            $ary = array('status'=>1,'str'=>'该用户不存在');
            $this->ajaxReturn($ary);
        }

    }

    //获取各等级奖金数目
    public function get_pratio(){
        $pratio =  M('pratio')->find();
        $params = [
            'rate_0' => floatval($pratio['pratio']),
            'rate_1' => floatval($pratio['agent_pratio']),
            'rate_2' => floatval($pratio['upper_agent_pratio']),
            'rate_3' => floatval($pratio['upper_upper_agent_pratio'])
        ];
        return $params;
    }


    public function send_push($uid,$tel,$commission){

        $client = new Client(C('JPUSH.APP_KEY'), C('JPUSH.MASTER_SECRET'));

        try {
            $client->push()
                ->setPlatform('android')
                ->addAlias($tel)
                ->androidNotification('紫薇阁温馨提示：您获得奖金'.$commission.'元', [
                    'extras' => [
                        'type' => '/index.php/home/index/commission/id/' . $uid
                    ]
                ])
                ->options(array(
                    'apns_production' => false,
                ))
                ->send();
        } catch (APIConnectionException $e) {
            // try something here
            //print $e;
        } catch (APIRequestException $e) {
            // try something here
            //print $e;
        }

    }

    public function commission()
    {
        if(IS_AJAX) {
            $pbonus = M('pbonus');
            $p = I('p', 1,'intval');

            $field = ['tel','recharge', 'moneys', 'ctime'];
            $list = $pbonus->where(['uid'=>UID])->page($p)->limit(10)->field($field)->select();
            //print_r($list);die;
            $this->ajaxReturn(V(1, '奖金列表', $list ? $list : []));
        } else {
            $id = $_GET['id'];
            //获取奖金总数和充值奖金总数
            $commissions = M('register')->where(['id'=>$id])->getField('commissions');
            $pcommission = M('pbonus')->where(['uid'=>$id])->sum('moneys');
            //print_r($pcommission);die;
            $this->assign('commissions',$commissions);
            $this->assign('pcommission',$pcommission);
            $this->display('commission');
        }

    }

}
